// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using SnapObjects.Commons;

namespace SnapObjects.Data.Odbc
{
    internal class OdbcAseAdoDbCommand : OdbcAdoDbCommandBase
    {
        public OdbcAseAdoDbCommand(
            [NotNull] string sqlText,
            IEnumerable<IAdoDbParameter> parameters,
            bool addExceptionToTransaction,
            CommandType commandType = CommandType.Text)
            : base(sqlText, parameters, addExceptionToTransaction, commandType)
        {
        }

        public override void ExecuteParseOnly(DataContext dataContext)
        {
            try
            {
                base.ExecuteParseOnly(dataContext);
            }
            catch
            {
                var command = this.CreateCommand(dataContext);

                command.CommandText = "set showplan off \n set noexec off";

                command.ExecuteNonQuery();

                throw;
            }
        }

        public override Task ExecuteParseOnlyAsync(DataContext dataContext, CancellationToken cancellationToken = default)
        {
            try
            {
                return base.ExecuteParseOnlyAsync(dataContext);
            }
            catch
            {
                var command = this.CreateCommand(dataContext);

                command.CommandText = "set showplan off \n set noexec off";

                command.ExecuteNonQueryAsync(cancellationToken);

                throw;
            }
        }

        protected override string BuildNoExecCommandText(DataContext context, string commandText)
        {
            var sb = new StringBuilder();

            sb.AppendLine("set showplan on");
            sb.AppendLine("set noexec on");
            sb.AppendLine(commandText);
            sb.AppendLine("set showplan off");
            sb.AppendLine("set noexec off");

            return sb.ToString();
        }

        protected override void AddNoMappingParameter(IDbCommand command, IAdoDbParameter adoDbParameter)
        {
            Check.NotNull(command, nameof(command));
            Check.NotNull(adoDbParameter, nameof(adoDbParameter));

            IDbDataParameter dataParameter = command.CreateParameter();
            dataParameter.ParameterName = adoDbParameter.ParameterName;

            dataParameter.Direction = adoDbParameter.Direction;

            if (adoDbParameter.Value == null)
            {
                dataParameter.Value = DBNull.Value;

                dataParameter.DbType = DbType.AnsiString;
            }
            else
            {
                dataParameter.Value = adoDbParameter.Value;
            }

            command.Parameters.Add(dataParameter);
        }
    }
}
